package com.uhu.code;

import java.util.Arrays;

/**
 * 41. 缺失的第一个正数
 * https://leetcode.cn/problems/first-missing-positive/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution41 {
    public int firstMissingPositive(int[] nums) {
        Arrays.sort(nums);
        int firstNum = -1;
        int i = 0;
        while(i < nums.length) {
            if(nums[i] > 0){
                firstNum = i;
                break;
            }
            i++;
        }
        if(firstNum == -1 || nums[firstNum] > 1) {
            return 1;
        }
        while (firstNum < nums.length - 1) {
            if(nums[firstNum + 1] - nums[firstNum] != 1) {
                return nums[firstNum] + 1;
            }
            firstNum ++;
        }
        return nums[nums.length - 1] + 1;
    }
}
